home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-06-23 | 6.2 KB | 180 lines |
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.AWTEventMulticaster;
-
- import com.apple.mrj.jdirect.PointerStruct;
-
- /**
- * Apple Worldwide Developer Technical Support
- *
- * Sample showing how to send and receive AppleEvents using JDirect 2.
- *
- * File: AESend.java
- *
- * This class contains the code needed to send an AppleEvent and
- * to extract any reply information from the AppleEvent.
- * The reply information is fired as an java.awt.event.ActionEvent
- * to all registered ActionListeners.
- *
- * @author Levi Brown
- * @author Michael Hopkins
- * @author Apple Computer, Inc.
- *
- * Copyright ©1999 Apple Computer, Inc.
- * All rights reserved.
- *
- * @version 1.0
- * 4/15/1999 Shipped as 'AppleEvent Send and Receive' sample.
- *
- * You may incorporate this sample code into your applications without
- * restriction, though the sample code has been provided "AS IS" and the
- * responsibility for its operation is 100% yours. However, what you are
- * not permitted to do is to redistribute the source as "Apple Sample
- * Code" after having made changes. If you're going to re-distribute the
- * source, we require that you make it clear in the source that the code
- * was descended from Apple Sample Code, but that you've made changes.
- */
- public class AESend implements TypesConstants, AppleEventConstants, AEDataModelConstants, ErrorConstants
- {
- /**
- * The maximum length of the keyDirectObject parameter of the Apple Event
- */
- protected static final int kMaxTextSize = 255;
- /**
- * The length of an OSType (int).
- */
- protected static final int kSizeOfOSType = 4;
- /**
- * The OSType (four character code) of the target application's signature.
- * 'TSnd' in this case.
- */
- protected static final byte[] kTestTargetSig = { (byte)'T',(byte)'R',(byte)'c',(byte)'v'}; //'TRcv'
- /**
- * The OSType (four character code) of the event class.
- * 'Test' in this case.
- */
- protected static final int kTestClass = 0x54657374; //'Test'
- /**
- * The OSType (four character code) of the event ID.
- * 'TsID' in this case.
- */
- protected static final int kTestID = 0x54734944; //'TsID'
- /**
- * A null callback handler for AESend
- */
- protected static final AEIdleClosureUPP kNoIdleProc = null;
- /**
- * A null callback handler for AESend
- */
- protected static final AEFilterClosureUPP kNoFilterProc = null;
- /**
- * The list of all registered ActionEvent listeners for this object.
- * @see #addActionListener
- * @see #removeActionListener
- * @see #fireActionEvent
- */
- protected ActionListener actionListener = null;
-
- /**
- * Sends a string to our target application via Apple Events.
- * @param theText, the string to pass along to the target application.
- * @exception if any problem occured while sending the AppleEvent.
- */
- public void sendAppleEvent(String theText) throws NativeException
- {
- AEAddressDescStruct targetAddrDesc = new AEAddressDescStruct();
- AppleEventStruct event = new AppleEventStruct();
- AppleEventStruct reply = new AppleEventStruct();
- short err;
- int[] actualType = new int[1];
- int[] actualSize = new int[1];
- byte[] replyText = new byte[kMaxTextSize];
- NativeException error = null;
-
- try
- {
- //Create a new descriptor using the target applications 4 character type
- err = AEDataModelFunctions.AECreateDesc(typeApplSignature, kTestTargetSig, kSizeOfOSType, targetAddrDesc);
- ErrorHandler.checkError(err, "Error returned by AECreateDesc()");
-
- //Create the apple event with the class, event id, and address of the apple event target.
- err = AEDataModelFunctions.AECreateAppleEvent(kTestClass, kTestID, targetAddrDesc, (short)kAutoGenerateReturnID, kAnyTransactionID, event);
- ErrorHandler.checkError(err, "Error returned by AECreateAppleEvent()");
-
- if (theText != null && !theText.equals("") ) // if string is not null, and not empty
- {
-
- //Turn the String into a byte array
- byte[] theData = theText.getBytes();
-
- //Put the string into the direct object parameter
- err = AEDataModelFunctions.AEPutParamPtr(event, keyDirectObject, typeChar, theData, theData.length);
- ErrorHandler.checkError(err, "Error returned by AEPutParamPtr()");
-
- //Send the apple event with an empty reply event. Wait for reply, user interaction allowed
- err = AppleEventFunctions.AESend(event, reply, kAEWaitReply + kAECanInteract + kAECanSwitchLayer, (short)kAENormalPriority, kAEDefaultTimeout, kNoIdleProc, kNoFilterProc);
-
- if ( err == connectionInvalid )
- {
- throw new NativeException("The target application is not running.", err);
- }
-
- ErrorHandler.checkError(err, "Error returned by AESend()");
-
- //Retrieve keyDirectObject parameter from reply structure
- err = AEDataModelFunctions.AEGetParamPtr(reply, keyDirectObject, typeChar, actualType, replyText, kMaxTextSize, actualSize);
-
- ErrorHandler.checkError(err, "Error returned by AEGetParamPtr()");
-
- //Create a String from the byte array of returned data.
- String text = new String(replyText, 0, actualSize[0]);
-
- //Fire an ActionEvent with the reply data so registered ActionListeners can see the AppleEvent being received.
- fireActionEvent(text);
- }
- }
- catch (NativeException exc)
- {
- error = exc;
- }
- finally
- {
- AEDataModelFunctions.AEDisposeDesc(targetAddrDesc);
- AEDataModelFunctions.AEDisposeDesc(event);
- AEDataModelFunctions.AEDisposeDesc(reply);
- }
-
- if (error != null)
- throw error;
- }
-
- /**
- * Adds the specified action listener to receive action events from this object.
- * @param l the action listener
- */
- public void addActionListener(ActionListener l)
- {
- actionListener = AWTEventMulticaster.add(actionListener, l);
- }
-
- /**
- * Removes the specified action listener so it no longer receives
- * action events from this object.
- * @param l the action listener
- */
- public void removeActionListener(ActionListener l)
- {
- actionListener = AWTEventMulticaster.remove(actionListener, l);
- }
-
- /**
- * Fire an action event to the listeners.
- * @param the action command associated with the event.
- * In this case it will be the text from the AppleEvent.
- */
- protected void fireActionEvent(String message)
- {
- if (actionListener != null)
- actionListener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, message));
- }
- }